<template>
  <div class="app-container">
    <v-form-render v-show="showSearch" :form-json="formJsonSearch" :form-data="formDataSearch" :option-data="optionDataSearch" ref="vFormRefSearch" >
    </v-form-render>




    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>



    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="Id" align="center" prop="id" />


      <el-table-column  :label= "item.label" :prop= "item.componentId" v-for="(item, index) in columnDefineList" :key="index" :align="item.align" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate2(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <v-form-render :form-json="formJson" :form-data="formData" :option-data="optionData" ref="vFormRef">
      </v-form-render>
      <el-button type="primary" @click="submitForm">保存</el-button>
    </el-dialog>

  </div>

</template>
<script>
import {getDefine} from "@/api/system/define";
import {listColumnListByDefineId} from "@/api/system/column";
import {listData,delData} from  "@/api/system/data";
import {addData,updateData,getData} from "@/api/system/data";

export default {
  data() {
    return {
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      /* 注意：formJson是指表单设计器导出的json，此处演示的formJson只是一个空白表单json！！ */
      formJsonSearch: {"widgetList":[],"formConfig":{"labelWidth":80,"labelPosition":"left","size":"","labelAlign":"label-left-align","cssCode":"","customClass":"","functions":"","layoutType":"PC","onFormCreated":"","onFormMounted":"","onFormDataChange":""}},
      formDataSearch: {},
      optionDataSearch: {},
      fid:null,
      columnDefineList:[],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
        isDelete: null,
        formId:null,
        searchForm:null
      },
      dataList:[],
      // 总条数
      total: 0,
      formJson: {"widgetList":[],"formConfig":{"labelWidth":80,"labelPosition":"left","size":"","labelAlign":"label-left-align","cssCode":"","customClass":"","functions":"","layoutType":"PC","onFormCreated":"","onFormMounted":"","onFormDataChange":""}},
      formData: {},
      optionData: {},
      did:null,
      // 是否显示弹出层
      open: false,
      title:""

    }
  },
  mounted() {
    this.fid = this.$route.query.fid
    this.queryParams.formId =  this.$route.query.fid
    getDefine(this.$route.query.fid).then(res=>{
      console.log(res.data.listSearchDefineDesc)
      this.$refs.vFormRefSearch.setFormJson(res.data.listSearchDefineDesc)
    })
    listColumnListByDefineId({defineId:this.fid}).then(res=>{
      this.columnDefineList =res.data
      console.log(this.columnDefineList)
      this.doSearch();
    })
    window['doSearch']=()=>{
      this.doSearch()
    }


  },
  methods: {
    doSearch() {
      this.$refs.vFormRefSearch.getFormData().then(formData => {
        console.log('formData')
        this.queryParams.searchForm=formData
        this.getList()


      }).catch(error => {
        // Form Validation failed
        this.$message.error(error)
      })
    },
    getList(){
      this.loading = true;

      listData(this.queryParams).then(res=>{
        console.log(res)
        this.dataList =res.rows
        this.total = res.total;
        this.loading = false;
      })
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //this.$router.push({path:"/tool/form" ,query:{fid:this.fid,did:'0'}});
      this.open = true;
      this.title = "新增";
      this.did =0
      getDefine(this.fid).then(res=>{
        console.log(res.data.formDesc)
        this.$refs.vFormRef.setFormJson(res.data.formDesc)
        this.$refs.vFormRef.setFormData({})
        // this.formJson = res.data.formDesc
      })

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids[0]
      this.$router.push({path:"/tool/form" ,query:{fid:this.fid,did:id}});
    },
    /** 修改按钮操作 */
    handleUpdate2(row) {
      const id = row.id || this.ids
      this.open = true;
      this.title = "修改";
      this.did =id
      getDefine(this.fid).then(res=>{
        console.log(res.data.formDesc)
        this.$refs.vFormRef.setFormJson(res.data.formDesc)
        if(id!=0){
          getData(id).then(res0=>{
            this.$nextTick(() => {
              this.$refs.vFormRef.setFormData(res0.data.formData)
            })
          })
        }
        // this.formJson = res.data.formDesc
      })

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/data/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    },
    /** 保存修改数据**/
    submitForm() {
      this.$refs.vFormRef.getFormData().then(formData => {
        let obj={};
        obj.formId=this.fid;
        obj.id =this.did
        obj.formData =formData;
        if(this.did==0){
          addData(obj).then(res=>{
            this.$message.success("已保存")
            this.open = false;
            this.getList();
          });
        }else {
          updateData(obj).then(res=>{
            this.$message.success("已保存")
            this.open = false;
            this.getList();
          });
        }
        // Form Validation OK
        alert( JSON.stringify(formData) )
      }).catch(error => {
        // Form Validation failed
        this.$message.error(error)
      })
    }
  }
}
</script>
